package com.smallsoft.phongtucvietnam;

import java.util.ArrayList;
import android.content.Context;
import com.smallsoft.adapter.MenuAdapter;
import com.smallsoft.beans.Category;
import com.smallsoft.helper.DatabaseHelper;
import com.smallsoft14.phongtucvietnam.R;

import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ProgressBar;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.view.LayoutInflater;


public class MainActivity extends ActionBarActivity {
	
	String TAG = "phongtucvietnam";
	DatabaseHelper dbHelper;
	DrawerLayout dLayout;
	ActionBarDrawerToggle mDrawerToggle;
	private CharSequence mDrawerTitle;
    private CharSequence mTitle;
	ListView listView;
	ArrayList<Category> categories = new ArrayList<Category>();
	MenuAdapter adapter;
	WebView webView;
	Handler mHandler = new Handler();
	ProgressBar progressBar;
	ProgressDialog progressDialog;
	ActionBar actionBar;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        actionBar = getSupportActionBar();
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP);
        actionBar.setIcon(R.drawable.toggle);
        actionBar.setDisplayHomeAsUpEnabled(false);
        actionBar.setHomeButtonEnabled(true);
        dLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
        webView = (WebView)findViewById(R.id.home_web_view);
        progressBar = (ProgressBar)findViewById(R.id.home_progress_bar);
        listView = (ListView)findViewById(R.id.left_drawer);
        dbHelper = new DatabaseHelper(this);
        categories = dbHelper.getCategory(this);
        adapter = new MenuAdapter(this, categories);
        listView.setAdapter(adapter);
        webView.setPadding(0, 0, 0, 0);
        webView.loadUrl("file:///android_asset/home.html");
        Log.d(TAG, ""+categories.get(0).getName());
        listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> apdaterView, View view, int position,
					long id) {
				// TODO Auto-generated method stub
				Bundle bundle = new Bundle();
				bundle.putInt("catId", categories.get(position).getId());
				bundle.putString("name", categories.get(position).getName());
				bundle.putInt("position", position);
				ContentFragment content = new ContentFragment();
				content.setArguments(bundle);
				FragmentTransaction trans = MainActivity.this.getSupportFragmentManager().beginTransaction();
				trans.replace(R.id.content_frame, content);
				trans.addToBackStack(null);
				trans.commit();
				dLayout.closeDrawers();
			}
		});
        mTitle = mDrawerTitle = getTitle();
        mDrawerToggle = new ActionBarDrawerToggle(this, dLayout, R.drawable.toggle, R.string.drawer_open, R.string.drawer_close){

        	
			@Override
			public boolean onOptionsItemSelected(MenuItem item) {
				// TODO Auto-generated method stub
				
				return super.onOptionsItemSelected(item);
			}

			@Override
			public void onDrawerClosed(View drawerView) {
				// TODO Auto-generated method stub
				super.onDrawerClosed(drawerView);
				getSupportActionBar().setTitle(mTitle);
                supportInvalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                Log.d(TAG, "Close");
			}

			@Override
			public void onDrawerOpened(View drawerView) {
				// TODO Auto-generated method stub
				super.onDrawerOpened(drawerView);
				getSupportActionBar().setTitle(mDrawerTitle);
                supportInvalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                Log.d(TAG, "Open");
			}
        };
        dLayout.setDrawerListener(mDrawerToggle);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
    	/*
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        switch (id) {
		case android.R.id.home:
			Log.d(TAG, "HOME CLICKED");
			dLayout.setDrawerListener(mDrawerToggle);
			return true;
		default:
			break;
		}
        */
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // If the nav drawer is open, hide action items related to the content view
        boolean drawerOpen = dLayout.isDrawerOpen(listView);
        //menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }
}
